/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.qzDictDetails.service;

import com.fzhucc.modules.qzDictDetails.domain.QzDictDetails;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.qzDictDetails.repository.QzDictDetailsRepository;
import com.fzhucc.modules.qzDictDetails.domain.dto.QzDictDetailsDto;
import com.fzhucc.modules.qzDictDetails.domain.dto.QzDictDetailsQueryCriteria;
import com.fzhucc.modules.qzDictDetails.service.mapstruct.QzDictDetailsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2024-04-24
**/
@Service
@RequiredArgsConstructor
public class QzDictDetailsService {

    private final QzDictDetailsRepository qzDictDetailsRepository;
    private final QzDictDetailsMapper qzDictDetailsMapper;

    public Map<String,Object> queryAll(QzDictDetailsQueryCriteria criteria, Pageable pageable){
        Page<QzDictDetails> page = qzDictDetailsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(qzDictDetailsMapper::toDto));
    }

    public List<QzDictDetailsDto> queryAll(QzDictDetailsQueryCriteria criteria){
        return qzDictDetailsMapper.toDto(qzDictDetailsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public QzDictDetailsDto findById(Integer id) {
        QzDictDetails qzDictDetails = qzDictDetailsRepository.findById(id).orElseGet(QzDictDetails::new);
        ValidationUtil.isNull(qzDictDetails.getId(),"QzDictDetails","id",id);
        return qzDictDetailsMapper.toDto(qzDictDetails);
    }

    @Transactional(rollbackFor = Exception.class)
    public QzDictDetailsDto create(QzDictDetails resources) {
        return qzDictDetailsMapper.toDto(qzDictDetailsRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(QzDictDetails resources) {
        QzDictDetails qzDictDetails = qzDictDetailsRepository.findById(resources.getId()).orElseGet(QzDictDetails::new);
        ValidationUtil.isNull( qzDictDetails.getId(),"QzDictDetails","id",resources.getId());
        qzDictDetails.copy(resources);
        qzDictDetailsRepository.save(qzDictDetails);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            qzDictDetailsRepository.deleteById(id);
        }
    }

    public void download(List<QzDictDetailsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (QzDictDetailsDto qzDictDetails : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("字典分类", qzDictDetails.getType());
            map.put("具体字典值", qzDictDetails.getDictValue());
            map.put("排序", qzDictDetails.getSort());
            map.put("具体字典名称", qzDictDetails.getDictLabel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}